// localLifePackage/pages/codeApply1/codeApply1.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        name: "入驻商管理中心"
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        that.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        that.setData({
            id: options.id
        });
        that.getBase();
        that.getMyInfo();
        that.getMerchantData();
    },
    getUserProfile() {
        console.log(222);
        var that = this;
        common.getUserInfoNew({
            success(res) {}
        });
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                var componentStyle = that.data.componentStyle;
                that.setData({
                    componentStyle: {
                        ...componentStyle,
                        color_minor: !res.extra.color_main ? "#FE513B" : res.extra.color_main,
                        color_minor: !res.extra.color_minor ? "#FE513B" : res.extra.color_minor
                    },
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    rp_name: res.extra.rp_name,
                    luck_img: res.extra.luck_img,
                    little_title: res.extra.title,
                    header_bg: res.extra.header_bg ? res.extra.header_bg : that.data.domain + "img/top.png",
                    leader_title: res.extra.leader_title
                });
            }
        });
    },
    getMyInfo: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                console.log(res);
                that.setData({
                    info: res.extra
                });
            }
        });
    },
    toWriteOff: function() {
        wx.navigateTo({
            url: "/localLifePackage/pages/writeOff/writeOff"
        });
    },
    scancode: function() {
        var that = this;
        wx.scanCode({
            onlyFromCamera: true,
            success(res2) {
                console.log(res2.result);
                common.request({
                    endpoint: "Localbusiness/order/order-detail-by-uid",
                    method: "post",
                    data: {
                        user_id: that.data.info.m_id,
                        code: res2.result
                    },
                    success: res => {
                        console.log(res);
                        if (res.extra.order_sn) {
                            wx.navigateTo({
                                url: "/localLifePackage/pages/writeOrder/writeOrder?code=" + res2.result + "&info=" + encodeURIComponent(JSON.stringify(res.extra))
                            });
                        } else {
                            wx.showModal({
                                title: "提示",
                                content: "核销码不正确，请重新输入",
                                showCancel: false,
                                success(res) {}
                            });
                            return;
                        }
                    }
                });
            }
        });
    },
    getMerchantData: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Localbusiness/common/local-data",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    details: res.extra
                });
                wx.setNavigationBarTitle({
                    title: res.extra.name
                });
            }
        });
    },
    goToLeaderFunction: function(e) {
        var that = this;
        wx.navigateTo({
            url: e.currentTarget.dataset.url + encodeURIComponent(JSON.stringify(that.data.details))
        });
    },
    goToMyOrder: function(e) {
        var that = this;
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    }
});